package com.ys.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ys.WebConstants;
import com.ys.extend.common.model.AjaxJson;
import com.ys.extend.utils.ContextHolderUtils;
import com.ys.web.pojo.SystemPermissonInfo;
import com.ys.web.remotemodel.AdOfflineInfo;
import com.ys.web.remotemodel.ContractInfo;
import com.ys.web.security.SystemPermissionUtils;
import com.ys.web.service.ContractInfoService;
import com.ys.web.ws.service.ManageContractInfoService;

/**
 * 合同信息-广告管理
 * @author wdj
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/contractInfoController")
public class ContractInfoController {
	
	@Autowired
	private ContractInfoService contractInfoService;
	
	
	/**
	 * 显示合同信息
	 * @param httpServletRequest
	 * @return
	 */
	@RequestMapping(params="showContractInfo")
	public ModelAndView showContractInfo(HttpServletRequest httpServletRequest){
		return new ModelAndView("contractInfo/contractInfoList");
	}
	
	/**
	 * 显示广告到期的信息
	 * @param httpServletRequest
	 * @return
	 */
	@RequestMapping(params="showAdOfflineInfo")
	public ModelAndView showAdOfflineInfo(HttpServletRequest httpServletRequest){
		return new ModelAndView("contractInfo/adOfflineInfoList");
	}
	
	
	
	/**
	 * 根据sem获取合同信息列表
	 * @param session
	 * @return 合同信息列表
	 */
	@RequestMapping(params="getContractInfoBySemUserId")
	@ResponseBody
	public List<ContractInfo> getContractInfoBySemUserId(HttpSession session) {
		List<ContractInfo> list = new ArrayList<>();
		
		String username=(String)session.getAttribute("username");
		Integer userId = (Integer)session.getAttribute("userId");
		SystemPermissonInfo systemPermissonInfo = SystemPermissionUtils.getUserPermission(username);
		if(username!=null&&userId!=null){
			try {
				if(systemPermissonInfo.getRoleId().contains(WebConstants.SUPER_USER)
						||systemPermissonInfo.getRoleId().contains(WebConstants.SEM_MANAGER_USER)){
					list = contractInfoService.getContractInfoBySemUserId(null);
				}else{
					list = contractInfoService.getContractInfoBySemUserId(userId);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return list;
	}
	
	/**
	 * 更新合同信息
	 * @param contractInfo
	 * @return
	 */
	@RequestMapping(params="updateContractInfo")
	@ResponseBody
	public AjaxJson updateContractInfo(@RequestBody ContractInfo contractInfo) {
		AjaxJson j = new AjaxJson();
		j.setMsg("更新信息成功");
		
		try {
			String username = ContextHolderUtils.getSessionUsername();
			/* 更新时更新合同负责的sem*/
			//sem才能更新员工名称
			if(!SystemPermissionUtils.getUserPermission(username).getRoleId().contains(WebConstants.SEM_MANAGER_USER)
				&&!SystemPermissionUtils.getUserPermission(username).getRoleId().contains(WebConstants.SUPER_USER)){
				contractInfo.setEmployeeName(SystemPermissionUtils.getUserPermission(username).getEmployeeName());
				contractInfo.setEmployeeNum(SystemPermissionUtils.getUserPermission(username).getEmplayeeNum());
			}
			
			
			contractInfoService.updateContractInfo(contractInfo);
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("更新失败，系统异常");
			e.printStackTrace();
		}
		
		return j;
	}
	
	/**
	 * 根据sem获取广告到期信息
	 * @param session
	 * @return
	 */
	@RequestMapping(params="getAdOfflineInfoBySemUserId")
	@ResponseBody
	public List<AdOfflineInfo> getAdOfflineInfoBySemUserId(HttpSession session) {
		List<AdOfflineInfo> list = new ArrayList<>();
		
		String username=(String)session.getAttribute("username");
		Integer userId = (Integer)session.getAttribute("userId");
		SystemPermissonInfo systemPermissonInfo = SystemPermissionUtils.getUserPermission(username);
		if(username!=null&&userId!=null){
			try {
				if(systemPermissonInfo.getRoleId().contains(WebConstants.SUPER_USER)
						||systemPermissonInfo.getRoleId().contains(WebConstants.SEM_MANAGER_USER)){
					list = contractInfoService.getAdOfflineInfoBySemUserId(null);
				}else{
					list = contractInfoService.getAdOfflineInfoBySemUserId(userId);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return list;
	}
	
	
	
}
